<?php

class P2C_ProjectData {

	var $initial = false;

	var $id;
    var $project_id;
    var $start_date;
	var $end_date;
	var $target;
    var $current;
    var $percentage;
    var $days_remaining;
    var $estimated_worth;
    var $shares;
    var $batch_id;

	public static function find( $args = '' ) {
        global $wpdb;
        
		$defaults = array(
			'post_status' => 'any',
			'posts_per_page' => -1,
			'offset' => 0,
			'orderby' => 'startdate',
			'order' => 'ASC',
            'projectid' => 0);

		$args = wp_parse_args( $args, $defaults );
        $order_by = $args['orderby'] . ' ' . $args['order'];

		$history = $wpdb->get_results($wpdb->prepare("SELECT id, project_id, start_date, end_date, target, current, days_remaining, estimated_worth, shares, batch_id 
        FROM p2c_project_data WHERE project_id = %d 
        ORDER BY $order_by
        LIMIT %d,%d;",$args['projectid'],$args['offset'],$args['posts_per_page']));
        
        $objs = array();

		foreach ( $history as $data )
			$objs[] = new self( $data );

		return $objs;
	}
    
    public static function rowcount( $projectid ) {
        global $wpdb;

		$datarows = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM p2c_project_data WHERE project_id = %d", $projectid));
        
		return $datarows;
	}
    
    public static function create( $project, $batch, $current, $days, $worth, $shares, $target ){
        $instance = new self();
    	$instance->batch_id = $batch->id;
        $instance->start_date = $batch->start_date;
        $instance->project_id = $project->id;
        $instance->target = $target;
        $instance->current = $current;
        $instance->days_remaining = $days;
        $instance->estimated_worth = $worth;
        $instance->shares = $shares;
        
    	return $instance;
    }
    
    public static function get( $project ) {
        global $wpdb;
        
		$dataid = $wpdb->get_var($wpdb->prepare("SELECT id FROM p2c_project_data WHERE end_date IS NULL AND project_id = %d;",$project->id));
        return new self($dataid);
    }

	public function __construct( $data = null ) {
        global $wpdb;
        
		$this->initial = true;
        $this->percentage = 0;
        
        if($data && !is_object($data)) {
            $data = $wpdb->get_row($wpdb->prepare("SELECT id, project_id, start_date, end_date, target, current, days_remaining, estimated_worth, shares, batch_id 
            FROM p2c_project_data WHERE id = %d",$data));
        }
        
		if ( $data ) {
			$this->initial = false;
			$this->id = $data->id;
			$this->project_id = $data->project_id;
			$this->start_date = $data->start_date;
			$this->end_date = $data->end_date;
            $this->target = $data->target;
            $this->current = $data->current;
            $this->days_remaining = $data->days_remaining;
            $this->estimated_worth = $data->estimated_worth;
            $this->shares = $data->shares;
            $this->batch_id = $data->batch_id;
            if($data->target > 0 ) {
                $this->percentage = min(round($data->current / $data->target * 100),100);
            }
		}
    }
    
	/* Save */

	function save() {
        global $wpdb; 
        
		if ( $this->initial ) {        
			$wpdb->insert("p2c_project_data", array(
               "project_id" => $this->project_id,
                "start_date" => $this->start_date,
                "target" => $this->target,
               "current" => $this->current,
               "days_remaining" => $this->days_remaining,
                "estimated_worth" => $this->estimated_worth,
                "shares" => $this->shares,
                "batch_id" => $this->batch_id
            ));
            
            $this->initial = false;
            $this->id = $wpdb->insert_id;
            
            // end the old record
            $enddate = new DateTime($this->start_date);
            $enddate->sub(new DateInterval('PT1S'));
            $wpdb->query( $wpdb->prepare("UPDATE p2c_project_data 
            SET end_date = %s 
            WHERE project_id = %d AND end_date IS NULL AND start_date <= %s", $enddate->format('Y-m-d H:i:s'), $this->project_id, $enddate->format('Y-m-d H:i:s') ) );

		} 

		return $this->id;
	}

}


?>